package xyz.xpman.prisonerfitness.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import java.util.Objects;

import xyz.xpman.prisonerfitness.R;
import xyz.xpman.prisonerfitness.activity.base.BaseActivity;
import xyz.xpman.prisonerfitness.config.Constant;

public class TrainingGuideVideoActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_training_guide_video);
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);

        init();
    }


    private void init() {
        final WebView webView = findViewById(R.id.web_view);
        // final String url = "https://m.bilibili.com/video/BV1ms411y7J5";
        final String url = getIntentExtra(Constant.EXTRA_VIDEO);

        final WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                // 返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                return true;
            }
        });

        // todo 2022/01/14 存在问题，视频无法自动播放
        webView.loadUrl(url);
    }


    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return super.keyToBack(keyCode, event);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return processMenuItemForBack(item);
    }

    @Override
    protected void callBack() {
        this.finish();
    }

}